﻿package com.wy.component.email;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.struts2.ServletActionContext;

import com.wy.component.properties.PropertiesUtil;

public class SendEmailCode {
	
	private static PropertiesUtil p = new PropertiesUtil();
	
	/**
	 * 简单的邮箱发送，不带附件
	 * @param email
	 * @param emailCode
	 */
	public static void simpleEmail(String email,String emailCode){
		try {
			SimpleEmail se = new SimpleEmail();
			
			
			se.setHostName("mail.shwally.com");
			se.addTo(email, email.substring(0, email.indexOf("@")));//例：zhusw@shwally.com,zhusw
			se.setFrom("pm@shwally.com", "pm");
			se.setAuthentication("pm", "Pm123456"); // "XXXXX"是上面163邮箱的那个用户名,即去掉"@163.com","****"是163邮箱的密码
			se.setSubject("找回密码邮箱验证码");//标题
			se.setCharset("GB2312");//中文的话,要设置这个属性
			se.setMsg("\t\n\n尊敬的用户：您好！\n\n您正在找回密码，验证码为:"+emailCode);
//			se.setMsg("您正在找回密码，验证码为:"+emailCode);//发送内容
			se.send();
//			System.out.println(ImageUtil.formatDateStr(new Date(), "yyyy-MM-dd HH:mm:ss")+"========="+email+"=========验证码："+emailCode+",邮箱验证码发送成功!");
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendEmailAPI(String email,String content){
		try {
			// 创建一个Email附件
//			EmailAttachment emailattachment = new EmailAttachment();
//			emailattachment.setURL(new URL("http://www.vanyapp.com:8865/conference/html/img/1778_logo(06-01-(06-30-11-28-01).jpg"));
//			emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
//			emailattachment.setDescription("This is Smile picture");
//			emailattachment.setName("万雍科技");
			// 创建一个email
			HtmlEmail multipartemail = new HtmlEmail();
			multipartemail.setHostName("mail.shwally.com");
			multipartemail.addTo(email, email.substring(0, email.indexOf("@")));//例：zhusw@shwally.com,zhusw
			multipartemail.setFrom("pm@shwally.com", "万雍科技");
			multipartemail.setAuthentication("pm", "Pm123456"); // "XXXXX"是上面163邮箱的那个用户名,即去掉"@163.com","****"是163邮箱的密码
			multipartemail.setSubject("万雍移动门户邮件接口");//标题
			// 设置图片的路径
			multipartemail.setCharset("gb2312");//中文要设置这个属性
			//Html格式
			StringBuffer sb=new StringBuffer();
			sb.append("<html>                                                                                                                             "); 
			sb.append("<body style='font-family: \"微软雅黑\"; '>                                                                                         "); 
			sb.append("	<div style='text-align:left;'>                                                                                                    "); 
			sb.append("		<p style='padding: 10px 0 0; margin: 0;line-height: 24px; word-break: break-all;'>"+content+"</p> "); 
			sb.append("		<div style=' color: #999; font-size: 12px;line-height: 8px;padding-bottom:20px;'>                                               ");
			sb.append("			<p>此为系统邮件，请勿回复</p>                                                                                                 ");
			sb.append("			<p>请保管好您的邮箱，避免账号被他人盗用。</p>                                                                                 ");
			sb.append("		</div>                                                                                                                          ");
			sb.append("		<div style='color: gray;'>                                                                                                      ");
			sb.append("			<div style='height: 1px; width: 260px; background: #dfdfdf; display: block; margin-bottom: 20px;'></div>                      ");
			sb.append("			<img src='http://www.vanyapp.com:8865/conference/html/img/1778_logo(06-01-(06-30-11-28-01).jpg' />                            ");
			sb.append("			<div style='font-size: 14px; color: #666; line-height: 11px; padding-left: 10px;'>                                            ");
			sb.append("				<p>万雍科技（上海）有限公司</p>                                                                                             ");
			sb.append("				<p style='font-style: oblique; color: #2495FF; font-size: 12px; line-height: 8px;'>Wally Tech.(Shanghai) Co.,Ltd.</p>       ");
			sb.append("				<p>地址：上海市徐汇区虹漕路421号虹漕园65号楼4楼B座</p>                                                                      ");
			sb.append("				<p>电话：021-34619929</p>                                                                                                   ");
			sb.append("				<p>传真：021-34619926</p>                                                                                                   ");
			sb.append("				<p>网址：www.shwally.com</p>                                                                                                ");
			sb.append("				<p>技术服务热线：400 820 3806</p>                                                                                           ");
			sb.append("			</div>                                                                                                                        ");
			sb.append("		</div>                                                                                                                          ");
			sb.append("	</div>                                                                                                                            ");
			sb.append("</body>                                                                                                                            ");
			sb.append("</html>                                                                                                                            ");
			multipartemail.setHtmlMsg(sb.toString());
			//文本格式
//			multipartemail.setTextMsg(emailCode);
			// 添加附件
//			multipartemail.attach(emailattachment);
			// 发送邮件
			multipartemail.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 复杂的邮箱发送，带附件
	 * @param email
	 * @param emailCode
	 * @throws MalformedURLException 
	 */
	public static void emailAttachment(String email,String emailCode) throws MalformedURLException{
		try {
			// 创建一个Email附件
//			EmailAttachment emailattachment = new EmailAttachment();
//			emailattachment.setURL(new URL("http://www.vanyapp.com:8865/conference/html/img/1778_logo(06-01-(06-30-11-28-01).jpg"));
//			emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
//			emailattachment.setDescription("This is Smile picture");
//			emailattachment.setName("万雍科技");
			// 创建一个email
			HtmlEmail multipartemail = new HtmlEmail();
			multipartemail.setHostName("mail.shwally.com");
			multipartemail.addTo(email, email.substring(0, email.indexOf("@")));//例：zhusw@shwally.com,zhusw
			multipartemail.setFrom("pm@shwally.com", "万雍科技");
			multipartemail.setAuthentication("pm", "Pm123456"); // "XXXXX"是上面163邮箱的那个用户名,即去掉"@163.com","****"是163邮箱的密码
			multipartemail.setSubject("找回密码邮箱验证码");//标题
			// 设置图片的路径
			multipartemail.setCharset("gb2312");//中文要设置这个属性
			//Html格式
			StringBuffer sb=new StringBuffer();
			sb.append("<htm>                                                                                                                                                                                                      ");
			sb.append("		<body>                                                                                                                                                                                                ");
			sb.append("				<center>                                                                                                                                                                                      ");
			sb.append("				<div style='text-align:left;'>                                                                                                                                                                ");
			sb.append("				<hr/>                                                                                                                                                                                         ");
			sb.append("					<br/><br/>                                                                                                                                                                                ");
			sb.append("					尊敬的用户：您好！<br/><br/>                                                                                                                                                                    ");
			sb.append("					您正在修改密码，请在验证码框输入：<font style='border-bottom:1px dashed gray;color:#FF6600;font-size:20;font-weight:bold'>"+emailCode+"</font>，以完成操作。                                                                                                                                                              ");
			sb.append("					<br/><br/>                                                                                                                                                                                ");
			sb.append("					<font color='gray'>注意：此操作可能会修改您的个人信息。如非本人操作，请及时完成操作以保证帐户安全</br>（工作人员不会向你索取此验证码，请勿泄漏！)</font><br/><br/>                                                                ");
			sb.append("				<hr/><br/><br/>                                                                                                                                                                               ");
			sb.append("				<font color='gray'>&nbsp;此为系统邮件，请勿回复<br/>&nbsp;请保管好您的邮箱，避免账号被他人盗用。<br/><img src='http://www.vanyapp.com:8865/conference/html/img/1778_logo(06-01-(06-30-11-28-01).jpg'/></font>     ");
			sb.append("			</div>                                                                                                                                                                                            ");
			sb.append("			</center>                                                                                                                                                                                         ");
			sb.append("		</body>                                                                                                                                                                                               ");
			sb.append("</html>                                                                                                                                                                                                    ");
			multipartemail.setHtmlMsg(sb.toString());
			//文本格式
//			multipartemail.setTextMsg(emailCode);
			// 添加附件
//			multipartemail.attach(emailattachment);
			// 发送邮件
			multipartemail.send();
			System.out.println("========="+email+"=========验证码："+emailCode+",邮箱验证码发送成功!");
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 
	 * Function Name               sendEmailWX                                   
	 * @throws Exception          		             
	 * @description 万雍科技邀请你关注微信企业号
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-4-21 上午11:40:20           黄忠                     Initial
	 **********************************************************************
	 */
	public static void sendEmailWX(String email) throws Exception{
		String guanzhuimg = p.getAppProperties("currentProjectUrl")+"/upload/qrcode/wxguanzhu.png";
		//邮件地址不能为null
		if(StringUtils.isEmpty(email)) {
			throw new Exception("SendEmailCode  sendEmailWX   入参email 邮件地址为null");
		}
		
		if(StringUtils.isEmpty(p.getAppProperties("enterpriseName"))) {
			throw new Exception("SendEmailCode  sendEmailWX   入参companyName 公司名称为null");
		}
		
		try {
			// 创建一个email
			HtmlEmail multipartemail = new HtmlEmail();
			multipartemail.addTo(email, email.substring(0, email.indexOf("@")));//例：zhusw@shwally.com,zhusw
			multipartemail.setHostName(p.getAppProperties("sendServer"));
			multipartemail.setFrom(p.getAppProperties("senderMail"), p.getAppProperties("enterpriseName"));
			multipartemail.setAuthentication(p.getAppProperties("senderAccount"), p.getAppProperties("senderSecret")); // "XXXXX"是上面163邮箱的那个用户名,即去掉"@163.com","****"是163邮箱的密码
			multipartemail.setSubject(p.getAppProperties("enterpriseName") + "邀请你关注微信企业号");//标题
			// 设置图片的路径
			multipartemail.setCharset("gb2312");//中文要设置这个属性
			//Html格式
			StringBuffer sb=new StringBuffer();
			sb.append("<html>");
			sb.append("<body>");
				sb.append("<div>");
				sb.append("<p><b>" + p.getAppProperties("enterpriseName") + "</b>已经开通微信企业号，关注后即可在微信收到企业发送的消息。");
					sb.append("	<br/>");
							sb.append("</p>");
							sb.append("<p>请在微信扫描以下二维码关注。");
							sb.append("<br/>（如果你是在<b>手机</b>上打开此邮件，请先保存二维码图片，进入微信'扫一扫'，选择右上角'相册'中的二维码关注。）");
						    sb.append("<br/>");
						    sb.append("</p>");
						    sb.append("<p><img src='"+guanzhuimg+"' width='200' height='200'></p>");
//						    sb.append("<p>如果你不是该企业/组织的员工或合作伙伴，你可以<a href='https://qy.weixin.qq.com/cgi-bin/mail_join?action=invitereject&amp;reject_code=027126ec2ac63bc6ad304b55fa83d71c&amp;t=mail_invite_reject_result'>不再接收此邀请</a>， 也可以");
//						    sb.append("<a href='https://qy.weixin.qq.com/cgi-bin/mail_join?action=reportillegal&amp;illegal_code=3540dd811ff87b5f1960d6781225044b&amp;t=mail_report_illegal_result'>举报此账号</a>。");
//						    sb.append("<br>");
//						    sb.append("</p>");
					 sb.append("</div>");
			sb.append("</body>");
			sb.append("</html>");
	        // 设置邮件的内容体                                                                                                                                                                 ");
			multipartemail.setHtmlMsg(sb.toString());
			//文本格式
//			multipartemail.setTextMsg(emailCode);
			// 添加附件
//			multipartemail.attach(emailattachment);
			// 发送邮件
			multipartemail.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
		

		
	} 
	
	
}
